#!/bin/bash

# MarkEdit Docker 构建脚本

set -e

# 配置
IMAGE_NAME="markedit"
IMAGE_TAG=${1:-latest}
FULL_IMAGE_NAME="$IMAGE_NAME:$IMAGE_TAG"

echo "开始构建 Docker 镜像: $FULL_IMAGE_NAME"

# 检查Docker环境
if ! command -v docker &> /dev/null; then
    echo "错误: 未找到Docker，请先安装Docker"
    exit 1
fi

# 检查Dockerfile
if [ ! -f "Dockerfile" ]; then
    echo "错误: 未找到Dockerfile"
    exit 1
fi

# 构建镜像
echo "正在构建镜像..."
docker build -t $FULL_IMAGE_NAME .

if [ $? -eq 0 ]; then
    echo "镜像构建成功: $FULL_IMAGE_NAME"
    
    # 显示镜像信息
    echo ""
    echo "镜像信息:"
    docker images $IMAGE_NAME:$IMAGE_TAG
    
    echo ""
    echo "使用方法:"
    echo "  # 运行容器"
    echo "  docker run -d -p 8080:8080 --name markedit-app $FULL_IMAGE_NAME"
    echo ""
    echo "  # 使用docker-compose"
    echo "  docker-compose up -d"
    echo ""
    echo "  # 查看日志"
    echo "  docker logs markedit-app"
    echo ""
    echo "  # 停止容器"
    echo "  docker stop markedit-app"
    echo ""
    echo "  # 删除容器"
    echo "  docker rm markedit-app"
else
    echo "镜像构建失败"
    exit 1
fi